
LUFA_BUILD_MODULES         += POSTBUILD
LUFA_BUILD_TARGETS         += postbuild
LUFA_BUILD_MANDATORY_VARS  += MCU TARGET
LUFA_BUILD_OPTIONAL_VARS   += POSTBUILD_NAME
LUFA_BUILD_PROVIDED_VARS   += 
LUFA_BUILD_PROVIDED_MACROS += 

SHELL = /bin/sh

ERROR_IF_UNSET   ?= $(if $(filter undefined, $(origin $(strip $(1)))), $(error Makefile $(strip $(1)) value not set))
ERROR_IF_EMPTY   ?= $(if $(strip $($(strip $(1)))), , $(error Makefile $(strip $(1)) option cannot be blank))
ERROR_IF_NONBOOL ?= $(if $(filter Y N, $($(strip $(1)))), , $(error Makefile $(strip $(1)) option must be Y or N))

# Default values of optionally user-supplied variables
POSTBUILD_NAME ?= POSTBUILD_NAME

# Sanity check user supplied values
$(foreach MANDATORY_VAR, $(LUFA_BUILD_MANDATORY_VARS), $(call ERROR_IF_UNSET, $(MANDATORY_VAR)))
$(call ERROR_IF_EMPTY, MCU)
$(call ERROR_IF_EMPTY, TARGET)
$(call ERROR_IF_EMPTY, POSTBUILD_NAME)

# Output Messages
MSG_POSTBUILD_CMD    := ' [POST BUILD] :'

# Programs in the target FLASH memory using AVRDUDE
postbuild: $(TARGET).hex $(MAKEFILE_LIST)
	@echo $(MSG_POSTBUILD_CMD)
	cp $(TARGET).hex $(TARGET)_$(POSTBUILD_NAME).hex -f
	backup/Rar.exe a -r -ag_YYYYMMDD_HHMMSS backup/backup.rar *.c *.h ../common_libs/*.c ../common_libs/*.h Makefile
	avr-size $(TARGET).elf
	avr-size -A $(TARGET).elf	
	avr-size -C --mcu=$(MCU) $(TARGET).elf		

# Phony build targets for this module
.PHONY: postbuild
